package PWS;

import PWS.Caesar;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;

public class Test {

    //public static Vinegere t = new Vinegere("Marco");
    //public static Caesar c = new Caesar("Marco");
    public static Vigenere t = new Vigenere("Marco", "slet");
    public static Vigenere q = null;
    public static Caesar c = new Caesar("Marco");

    public static void main(String[] args) {
        String path = "C:/ENC2.txt";
        
        RSA enc = new RSA("Mourad");
        String encrypted = enc.encrypt("Mexime is lief").toString();
        System.out.println(encrypted);
        
        RSA dec = new RSA("Mourad");
        String decrypted = dec.decrypt(enc.encrypt("Mexime is lief"));
        System.out.println(decrypted);
        
        //String enc = readFile(path);
        //System.out.println("Text: " + enc);
        //String enc2 = enc.replaceAll("\\s+", "(").replaceAll("\\(", " ").replaceAll(" ", "").trim();
        //System.out.println("enc2: " + enc2);
        //BigInteger bi_enc = new BigInteger(enc);
        //RSA RSA_dec = new RSA(499);
        //String decrypted = RSA_dec.decrypt(bi_enc);
        //System.out.println("decrypted:   "  + decrypted);

    }
    
    public static int hex2decimal(String s) {
        String digits = "0123456789ABCDEF";
        s = s.toUpperCase();
        int val = 0;
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            int d = digits.indexOf(c);
            val = 16*val + d;
        }
        return val;
    }

    public static String readFile(String path) {
        String s = "";
        StringBuilder contents = new StringBuilder();
        File file = new File(path);
        FileInputStream fis = null;
        BufferedInputStream bis = null;
        DataInputStream dis = null;

        try {
            fis = new FileInputStream(file);
            bis = new BufferedInputStream(fis);
            dis = new DataInputStream(bis);

            while (dis.available() != 0) {
                s = dis.readLine();
                contents.append(s);
                contents.append(System.getProperty("line.separator"));
            }

            fis.close();
            bis.close();
            dis.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    /**
     * Prints some data to a file
     */
    public static void writeToFile(String s, String filename) {

        BufferedOutputStream bufferedOutput = null;

        try {

            //Construct the BufferedOutputStream object
            bufferedOutput = new BufferedOutputStream(new FileOutputStream(filename));

            //Start writing to the output stream
            bufferedOutput.write(s.getBytes());
            //bufferedOutput.write("\n".getBytes());


        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            //Close the BufferedOutputStream
            try {
                if (bufferedOutput != null) {
                    bufferedOutput.flush();
                    bufferedOutput.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }

    Test(SecretKey key) {
        try {
            ecipher = Cipher.getInstance("DES");
            dcipher = Cipher.getInstance("DES");
            ecipher.init(Cipher.ENCRYPT_MODE, key);
            dcipher.init(Cipher.DECRYPT_MODE, key);

        } catch (javax.crypto.NoSuchPaddingException e) {
        } catch (java.security.NoSuchAlgorithmException e) {
        } catch (java.security.InvalidKeyException e) {
        }
    }
    Cipher ecipher;
    Cipher dcipher;

    public String encrypt(String str) {
        try {
            // Encode the string into bytes using utf-8
            byte[] utf8 = str.getBytes("UTF8");
            // Encrypt
            byte[] enc = ecipher.doFinal(utf8);

            // Encode bytes to base64 to get a string
            return new sun.misc.BASE64Encoder().encode(enc);
        } catch (javax.crypto.BadPaddingException e) {
        } catch (IllegalBlockSizeException e) {
        } catch (UnsupportedEncodingException e) {
        } catch (java.io.IOException e) {
        }
        return null;
    }

    public String decrypt(String str) {
        try {
            // Decode base64 to get bytes
            byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer(str);

            // Decrypt
            byte[] utf8 = dcipher.doFinal(dec);

            // Decode using utf-8
            return new String(utf8, "UTF8");
        } catch (javax.crypto.BadPaddingException e) {
        } catch (IllegalBlockSizeException e) {
        } catch (UnsupportedEncodingException e) {
        } catch (java.io.IOException e) {
        }
        return null;
    }
}